<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\MvcEvent;
use Mail\Mail;
use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;

class AbstractController extends AbstractActionController
{

    public function onDispatch(MvcEvent $e)
    {
        parent::onDispatch($e);
        $ssemail=$this->getAuthSession('email');
        if($ssemail==false){
            return $this->redirect()->toUrl('/');
        }
         
    }

    public function getListMapper()
    {
        return $this->getServiceLocator()->get('Auth\Model\ListMapper');
    }

    public function redirectToHome()
    {
        return $this->redirect()->toUrl('/');
    }

    public function getSettingMapper()
    {
        return $this->getServiceLocator()->get('Auth\Model\SettingMapper');
    }

    public function getUserMapper()
    {
        return $this->getServiceLocator()->get('Auth\Model\UserMapper');
    }

    public function getAuthService()
    {
        return $this->getServiceLocator()->get('FEAuthService');
    }

    public function setAuthSession($authData)
    {
        if (isset($authData['password']))
            unset($authData['password']);
        if (isset($authData['password_salt']))
            unset($authData['password_salt']);
        $ss=$this->getAuthService();
        $ss->getStorage()->write($authData);
    }
    public function getAuthSession($key = null){
        $ss = $this->getAuthService()->getIdentity();
        
        if($key != null){
            return isset($ss[$key]) ?  $ss[$key] : false;
        }
        return $ss;
    }
}